﻿using MESPro.DBManager;
using MESPro.Enums;
using MESPro.Models;
//using MESPro.Models;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MESPro.Global
{
    public static class CommonEX
    {
        /// <summary>
        /// 多语言支持
        /// </summary>
        public static void InitLanguage(ToolStripMenuItem tsmi, Func<Task> task)
        {
            var ss = Enum.GetNames(typeof(Language));
            foreach (var s in ss)
            {
                var LanMenuItem = new ToolStripMenuItem();
                LanMenuItem.Text = s;
                LanMenuItem.Name = s;
                LanMenuItem.Click += async (sender, e) =>
                {
                    var obj = sender as ToolStripMenuItem;
                    if (Enum.TryParse(obj!.Text, true, out Language la))
                    {
                        if (App.User is not null)
                        {
                            App.User.Language = la;
                            await task.Invoke();
                        }
                    }
                };
                tsmi.DropDownItems.Add(LanMenuItem);
            }

        }

        /// <summary>
        /// 将所有图标加载到清单中
        /// </summary>
        /// <param name="imageList"></param>
        public static void Init(this ImageList imageList)
        {
            imageList.ColorDepth = ColorDepth.Depth32Bit;
            imageList.ImageSize = new Size(32, 32);
            imageList.TransparentColor = Color.Transparent;
            string[] exts = { ".ico", ".png" };
            var files = Directory.GetFiles("img")
                .Where((x) =>
                {
                    return exts.Contains(Path.GetExtension(x));
                }).ToArray();
            for (int i = 0; i < files.Length; i++)
            {
                imageList.Images.Add(Image.FromFile(files[i]));
                imageList.Images.SetKeyName(i, Path.GetFileName(files[i]));
            }
            //var ss = imageList.Images;
        }

        /// <summary>
        /// 初始化树,根据List FunctionGroup 集合
        /// </summary>
        /// <param name="tree"></param>
        /// <param name="functionGroups"></param>
        /// <param name="isAdd"></param>
        public static async void InitTreeView(TreeView tree, List<FunctionGroup> functionGroups, bool isAdd = false)
        {
            if (isAdd)
            {

            }
            else
            {
                tree.Nodes.Clear();
            }
            tree.Nodes.Clear();
            foreach (FunctionGroup functionGroup in functionGroups)
            {
                TreeNode Tree1 = new TreeNode();
                Tree1.Text = functionGroup.DisName + "    ";
                Tree1.Tag = functionGroup;//似乎没有用..
                Tree1.ImageKey = functionGroup.ImageKey;
                Tree1.StateImageKey = functionGroup.ImageKey;
                Tree1.SelectedImageKey = functionGroup.ImageKey;
                Tree1.NodeFont = new System.Drawing.Font("隶书", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));


                foreach (Function function in functionGroup.Functions)
                {
                    TreeNode Tree2 = new TreeNode();
                    Tree2.Text = function.DisName;
                    //ufo 获取控件结构的树
                    var ControlTree = await Task.Run(() =>
                    {
                        return DbContext.Db.Queryable<DbControl>()
                        .Where(g => g.FunctionId == function!.Id)
                        .ToTree(it => it.Child, it => it.ParentId, 0);
                    });
                    function.TreeControls = ControlTree;
                    Tree2.Tag = function;
                    Tree2.ImageKey = function.ImageKey;
                    Tree2.StateImageKey = function.ImageKey;
                    Tree2.SelectedImageKey = function.ImageKey;
                    Tree1.Nodes.Add(Tree2);
                }
                tree.Nodes.Add(Tree1);
            }
        }
    }
}
